package com.zyz.bookborrowing.servlet;

import com.zyz.bookborrowing.bean.LendList;
import com.zyz.bookborrowing.service.ILendService;
import com.zyz.bookborrowing.service.LendServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "lendListServlet", urlPatterns = "/lendListServlet")
public class LendListServlet extends HttpServlet {

    ILendService lendService = new LendServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("borrow".equals(type)){
//            借阅，获取：1.借书卡编号 2.书籍id
            String cardId = req.getParameter("cardId");
            String bookId = req.getParameter("bookId");
            LendList lendList = new LendList();
            lendList.setBookId(Integer.parseInt(bookId));
            lendList.setCardId(cardId);
            lendList.setLendDate(new Date()); // 获取借书时间
//            保存借阅记录
            lendService.borrowLendList(lendList);
            resp.sendRedirect("/readerCardServlet?type=query");

        } else {
//            归还
            String cardId = req.getParameter("id");
//            根据cardId查询到对应的
            LendList lendList = lendService.queryByCardId(cardId);
            lendService.backLendList(lendList);
            resp.sendRedirect("/readerCardServlet?type=query");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
